define(["jquery","bbcare","comm/form/decode"],function($,$bb,$decode){
	var quest = {};

	/**
	 * 数据处理(若不需要数据处理，可以不写)
	 */
    quest.formatData = function(obj){
        var curTime = $bb.simple.getCurTime().date;
        if( obj.timeLimit == "afterNow"){
            obj.min = curTime;
        }else if( obj.timeLimit == "beforeNow" ){
            obj.max = curTime;
        }
		return obj;
	}

	/**
	 * tpl模板
	 */
	quest.tpl = function(){
		return 	'<script id="tpl_question_date" type="text/html">'+
				'	<div {{if relationId}}relationId="{{relationId}}"{{/if}} class="qaCard form-group form-group-sm col-sm-{{length}} col-md-{{length}} grid-label-{{labelLength}}">'+
				'		<label class="control-label {{align}}">{{label}}</label>'+
				'   	<div class="control-input">'+
				'			{{if status=="default"}}'+
				'   			<input type="date" class="form-control validatebox" name="{{name}}" min="{{min}}" max="{{max}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" data-validate="{{validate}}" data-attention="{{attention}}">'+
				'			{{else if status=="readonly"}}'+
				'				<p class="form-control-static validatebox" name="{{name}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" data-validate="{{validate}}" data-attention="{{attention}}"></p>'+
				'			{{else if status=="disabled"}}'+
				'   			<input type="date" class="form-control validatebox" name="{{name}}" min="{{min}}" max="{{max}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" data-validate="{{validate}}" data-attention="{{attention}}" disabled>'+
				'			{{/if}}'+
				'		</div>'+
				'	</div>'+
				'</script>';
	}

	/**
	 * 赋值
	 */
	quest.setValue = function($dom,value){
		if( value ){
			if( value.quesValue && value.quesValue == "today" ) value.quesValue = $bb.simple.getCurTime().date;
			$dom.hasClass("form-control-static") ? $dom.html(value.quesValue) : $dom.val(value.quesValue);
		}
	}

	/**
	 * 获取value（保存时，获取每个dom对应的答案）
	 */
	quest.getValue = function($dom,name){
		var value = {};
		value.quesValue = $dom.hasClass("form-control-static") ? $dom.html() : $dom.val();
		value.quesId = $dom.attr("data-quesId");
		value.deCode = $dom.attr("data-deCode");
		value.innDeCode = $dom.attr("data-innDeCode");
		var dataUnit = $dom.attr("data-dataUnit");
		if( dataUnit ) value.dataUnit = dataUnit;
		return value;
	}

	/**
	 * 事件函数
	 */
	quest.event = function(params,id){
		var lastMensesDateName = $decode.getDeCodeNameByName("lastMensesDate");
		var expectDateName = $decode.getDeCodeNameByName("expectDate");
		var $lastMensesDate = $("#" + id).find("input[type=date][name='" + lastMensesDateName + "']");
		var $expectDate = $("#" + id).find("input[type=date][name='" + expectDateName + "']");
		if( $lastMensesDate.length == 1 && $expectDate.length == 1 ){
			$lastMensesDate.unbind("input");
			$lastMensesDate.data("data",{"lastMensesDateDom":$lastMensesDate,"expectDateDom":$expectDate,"id":id});
			$lastMensesDate.bind("input",onInput);
		}
	}

	/**
	 * 末次月经计算预产期
	 */
	var onInput = function(){
		var data = $(this).data("data");
		var $lastMensesDate = data.lastMensesDateDom;
		var $expectDate = data.expectDateDom;
		var date1 = $lastMensesDate.val();
		if( date1 ){
			var time = new Date(date1).getTime();
			var newTime = time - 280*24*60*60*1000;
			var val = $bb.simple.getDateFormat(new Date(newTime)).date;
			if( $expectDate.hasClass("form-control-static") ){
				$expectDate.html(val);
			}else{
				$expectDate.val(val);
			}
		}else{
			$expectDate.val("");
		}
	}

	return quest;
});